Skip to content

fix: run signal effects inline only when session is locked - #27188

Closed
totally-not-ai[bot] wants to merge 2 commits into
mainfrom
fix/element-effect-run-inline-only-with-lock
Closed

totally-not-ai[bot] wants to merge 2 commits into
mainfrom
fix/element-effect-run-inline-only-with-lock

Conversation

@totally-not-ai

Copy link
Copy Markdown
Contributor

Fixes #27187

behavior deviation · flow-server · apps that change a signal after the request has released the session lock, e.g. in a request end listener

An element effect or a signal result callback ran right away whenever its UI was the current UI. After a request ends, the session lock is already released but the request's UI is still current, so the code ran without the lock and failed with "Cannot access state in VaadinSession or UI without locking the session". Such runs now go through UI.access like changes from any other thread. Workaround before this fix: clear the current instances around the signal write.

Risks:

  • ⚠️ Behaviour change: an effect re-run caused by a change made without the session lock now reports isBackgroundChange() == true, consistent with it now being dispatched like a background change.
  • ⚠️ Public API: added UIInternals.isCurrentAndLocked() in the internal component.internal package so the three places share one check.
  • 🧵 Threading: code that ran inline without the lock now runs later through UI.access; code that holds the lock is unchanged.
  • ✅ No breaking change, security, memory, serialization, performance or migration impact.

Context. The inline path exists so that changes made during a request apply in the same round trip. It only checked the current UI, which was enough before request end listeners and interceptors started running code after the lock is released.

  • Added UIInternals.isCurrentAndLocked(): the UI is current and its session is locked by the current thread.
  • ElementEffect runs an effect inline only when that holds, both in the dispatcher and in its test-environment fallback; otherwise it uses ui.access.
  • The signal result notifier created in VaadinService uses the same check.
  • Effect uses the same check to decide whether a change is a background change.
  • Added tests for an effect and a result notifier triggered with the UI current but the session unlocked; EffectTest background-change tests now use a real UI instead of a mock.

An effect ran inline whenever its UI was the current UI, also after
the request had released the session lock, e.g. in a request end
listener, and then failed on the lock check.

Fixes #27187
@totally-not-ai

Copy link
Copy Markdown
Contributor Author

Type of change

  • Bugfix

@sonarqubecloud

Copy link
Copy Markdown

@github-actions

Copy link
Copy Markdown
Contributor

Test Results

 1 698 files  ±0   1 788 suites  ±0   2h 11m 31s ⏱️ + 1m 20s
13 675 tests +2  13 603 ✅ +2  72 💤 ±0  0 ❌ ±0 
14 353 runs  +2  14 281 ✅ +2  72 💤 ±0  0 ❌ ±0 

Results for commit 4ed85a2. ± Comparison against base commit d216466.

@totally-not-ai

Copy link
Copy Markdown
Contributor Author

Closing in favour of fixing the root cause instead of adding a lock check to effects: a current UI must mean its session is locked by this thread (see the rules in #27198). #27201 restores the current instances when request handlers release the lock, and #27439 fires the session lock released event without a current UI. Together they cover #27187.

@totally-not-ai totally-not-ai Bot closed this Nov 26, 2026
@Artur-

Artur- commented Nov 27, 2026 •

Copy link
Copy Markdown
Member

@totally-not-ai Would it make sense to still apply this as a precaution and to avoid signals to work incorrectly even if the environment is incorrectly set up?

@totally-not-ai

Copy link
Copy Markdown
Contributor Author

@Artur- Yes, it is worth keeping as a precaution. It is cheap and only kicks in when the rule "current UI means locked session" is already broken.

vaadin-bot pushed a commit that referenced this pull request Nov 30, 2026
…CP: 25.4)

Related to #27187, #27188

**Hardening** · flow-server · signal effects owned by a UI

Signal effects, the signal result notifier and the background change
flag treated "this UI is current" as "this thread holds the session
lock". Flow's request handling now keeps that rule (#27201, #27439).
Code outside Flow can still break it, for example by calling
`UI.setCurrent` without locking the session. Then an effect touched UI
state without the lock and failed with "Cannot access state in
VaadinSession or UI without locking the session". After that, its error
handler failed the same way.

**Risks:**
- ⚠️ Behaviour change, only when the UI is current but its session is
not locked by this thread: the effect, or the result notifier task, is
queued through `UI.access` instead of running right away.
`isBackgroundChange()` reports `true` for that run.
- When the UI is current and locked, which is every normal case, nothing
changes.
- ✅ No new public API. The shared check is
`UIInternals.isCurrentAndLocked()`, which is in the internal package.
The added cost is one `hasLock()` call.

- `ElementEffect`, the result notifier in `VaadinService` and `Effect`'s
background change flag use the new check.
- `guidelines/architecture.md` now describes signal effects as checking
the session lock instead of trusting the current UI.
- Three existing `EffectTest` owner-UI tests now use a plain `UI` with a
mocked session that reports the lock as held, instead of a Mockito `UI`
mock. The owner UI's internals and session lock are now read, and a
Mockito `UI` has no internals. They avoid `MockUI` because it starts a
`VaadinService`, which registers a signal environment that stays active
for the rest of the test class.
- Tests: an effect and a result notifier task triggered while the owner
UI is current but unlocked run later with the lock held, and the effect
reports a background change. Both tests fail without the fix.

Working on behalf of @Artur-

---------

Co-authored-by: totally-not-ai[bot] <290682512+totally-not-ai[bot]@users.noreply.github.com>
Co-authored-by: caalador <mikael.grankvist@vaadin.com>
mshabarov pushed a commit to OriginalFelix/flow that referenced this pull request Dec 1, 2026
)

Related to vaadin#27187, vaadin#27188

**Hardening** · flow-server · signal effects owned by a UI

Signal effects, the signal result notifier and the background change
flag treated "this UI is current" as "this thread holds the session
lock". Flow's request handling now keeps that rule (vaadin#27201, vaadin#27439).
Code outside Flow can still break it, for example by calling
`UI.setCurrent` without locking the session. Then an effect touched UI
state without the lock and failed with "Cannot access state in
VaadinSession or UI without locking the session". After that, its error
handler failed the same way.

**Risks:**
- ⚠️ Behaviour change, only when the UI is current but its session is
not locked by this thread: the effect, or the result notifier task, is
queued through `UI.access` instead of running right away.
`isBackgroundChange()` reports `true` for that run.
- When the UI is current and locked, which is every normal case, nothing
changes.
- ✅ No new public API. The shared check is
`UIInternals.isCurrentAndLocked()`, which is in the internal package.
The added cost is one `hasLock()` call.

- `ElementEffect`, the result notifier in `VaadinService` and `Effect`'s
background change flag use the new check.
- `guidelines/architecture.md` now describes signal effects as checking
the session lock instead of trusting the current UI.
- Three existing `EffectTest` owner-UI tests now use a plain `UI` with a
mocked session that reports the lock as held, instead of a Mockito `UI`
mock. The owner UI's internals and session lock are now read, and a
Mockito `UI` has no internals. They avoid `MockUI` because it starts a
`VaadinService`, which registers a signal environment that stays active
for the rest of the test class.
- Tests: an effect and a result notifier task triggered while the owner
UI is current but unlocked run later with the lock held, and the effect
reports a background change. Both tests fail without the fix.

Working on behalf of @Artur-

---------

Co-authored-by: totally-not-ai[bot] <290682512+totally-not-ai[bot]@users.noreply.github.com>
Co-authored-by: caalador <mikael.grankvist@vaadin.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Hackathon 25.4: A signal effect runs without the session lock when a RequestEndedEvent listener changes a shared signal

1 participant